package top.yokey.android.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.AlphabetIndexer;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import activity.PictureMimeType;
import activity.PictureSelector;
import top.yokey.android.R;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.base.GlideEngine;

public class DynamicAddActivity extends BaseActivity {

    private AppCompatTextView sendTextView;
    private AppCompatEditText contentEditText;
    private AppCompatImageView[] mainImageView = new AppCompatImageView[9];

    private List<LocalMedia> imageList = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_dynamic_add);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        sendTextView = findViewById(R.id.sendTextView);
        contentEditText = findViewById(R.id.contentEditText);
        mainImageView[0] = findViewById(R.id.main1ImageView);
        mainImageView[1] = findViewById(R.id.main2ImageView);
        mainImageView[2] = findViewById(R.id.main3ImageView);
        mainImageView[3] = findViewById(R.id.main4ImageView);
        mainImageView[4] = findViewById(R.id.main5ImageView);
        mainImageView[5] = findViewById(R.id.main6ImageView);
        mainImageView[6] = findViewById(R.id.main7ImageView);
        mainImageView[7] = findViewById(R.id.main8ImageView);
        mainImageView[8] = findViewById(R.id.main9ImageView);

    }

    @Override
    public void initEvent() {
        for (int i = 0; i < mainImageView.length; i++) {
            final int pos = i;
            mainImageView[i].setOnClickListener(view -> {
                if (imageList.size() != 0 && imageList.get(pos) !=null) {
                    new AlertDialog.Builder(this)
                            . setTitle( "请选择~")
                            . setMessage( "您需要进行的操作")
                            . setPositiveButton( "删除",(dialogInterface, m) -> {

                            })
                            .setNegativeButton(  "更换", (dialogInterface, m) -> {

                            })
                            .show();

                }
                if (pos !=imageList.size()) return;
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .maxSelectNum(9 - imageList.size())
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(List<LocalMedia> result) {

                                boolean have = false;
                                for (int j = 0; j < result.size(); j++) {
                                    boolean flag = false;
                                    for (int k = 0; k < imageList.size(); k++) {
                                        if (imageList.get(k).getPath().equals(result.get(j).getPath())) {
                                            have = true;
                                            flag = true;
                                        }
                                    }
                                    if (!flag) {
                                        imageList.add(result.get(j));
                                    }
                                }
                                if (have) {
                                    Toast.makeText(DynamicAddActivity.this, "重复图片已去除",Toast.LENGTH_SHORT).show();
                                }
                                handlerImage();
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
            });
        }
    }

    private void handlerImage() {

        for (int i = 0; i < imageList.size(); i++) {
            Glide.with(this).load(imageList.get(i).getPath()).into(mainImageView[i]);
        }
        //只选了一张图片的时候 imageList.size 的值就是 1
        //数组下标从0
        if (imageList.size() < 9) {
            mainImageView[imageList.size()].setImageResource(R.mipmap.ic_add_image);
        }

    }

}